package com.javajun;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.context.junit4.SpringRunner;

import com.javajun.entity.Admin;
import com.javajun.entity.SysRole;
import com.javajun.service.AdminService;
import com.javajun.service.SysRoleService;

/**
 * @className: SysRoleTest
 * @description: 用户角色测试类
 * @author: JavaJun
 * @date: 2018年11月16日
 */
// 表示如何运行这个测试类
@RunWith(SpringRunner.class)
// 表示这个类是一个测试类
@SpringBootTest
public class FirsstInit {

    @Autowired
    private SysRoleService roleService;
    
    @Autowired
    private AdminService adminService;
    
    @Autowired
    private SysRoleService sysRoleService;
    
    @Test
    public void firsInit() {
        addRoleBase();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("这要是都能异常的话，简直无解，没救了~~");
        }
        addAdminBase();
    }
    
    /**
     * 1.用户角色测试类
     */
    public void addRoleBase() {
        // 管理员角色
        SysRole r1 = new SysRole();
        r1.setName("管理员");
        r1.setCode("ROLE_ADMIN");
        roleService.save(r1);
        
        // 学生角色
        SysRole r2 = new SysRole();
        r2.setName("学生");
        r2.setCode("ROLE_STUDENT");
        roleService.save(r2);
        
        // 教师
        SysRole r3 = new SysRole();
        r3.setName("教师");
        r3.setCode("ROLE_TEACHER");
        roleService.save(r3);
    }
    
    public void addAdminBase() {
        Admin admin = new Admin();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        admin.getSysRole().add(sysRoleService.getAdminRole());
        admin.setUsername("admin");
        admin.setPassword(encoder.encode("admin"));
        admin.setEmail("10086@qq.com");
        admin.setPhone("010-1234567");
        admin.setName("江流儿");
        adminService.save(admin);
    }
}
